package com.android.xunxian.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.Toast;

import com.android.xunxian.HomeActivity;
import com.android.xunxian.utils.UConstants;
import com.android.xunxian.utils.Util;
import com.oa.android.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**启动页
 * Created by zouqh on 2016/5/23.
 */
public class WelcomeActivity extends Activity {
    private Util util;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.Ext.init(getApplication());
        // 设置是否输出debug
        x.Ext.setDebug(true);
        setContentView(R.layout.weicome_activity);
        util = new Util(this);
        String name = util.getUser("name");
        String password = util.getUser("password");
        String mindpass = util.getUser("mindpass");
        String strIp=util.getUser("ip");
        String strPost=util.getUser("post");
        Util.printLog("strIp-----------------", strIp);
        Util.printLog("strPost-----------------", strPost);
        if (Util.NoNull(strIp)&&Util.NoNull(strPost)){
            UConstants.WEB_URL="http://"+strIp+":"+strPost+"/BLL/";
            new UConstants().setIp();
        }

        if (Util.NoNull(mindpass) && mindpass.equals("1")) {
            Login(name, password);
        } else {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                    finish();
                }
            }, 2000);
        }

    }



    //用户登录
    private void Login(final String name, final String pass) {
        RequestParams params = new RequestParams(UConstants.LOGIN_URL);
        params.addBodyParameter("LKind", "CBJ");
        params.addBodyParameter("EmpId", name);
        params.addBodyParameter("Pwd", pass);
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Util.printLog("getMerchants------------onSuccess-----", result);
                if (result.length() > 0) {
                    try {
                        JSONObject object = new JSONObject(result);
                        boolean success = object.getBoolean("success");
                        String msg ;
                        if (success) {

                            UConstants.EmpId = object.getString("EmpId");
                            UConstants.EmpName = object.getString("EmpName");
                            UConstants.RoleId = object.getString("RoleId");
                            UConstants.RoleName = object.getString("RoleName");
                            UConstants.Powers = object.getString("Powers");
                            if (object.toString().contains("ShowTBSL")){
                                UConstants.ShowTBSL= object.getString("ShowTBSL");
                            }
                            UConstants.PartId = object.getString("PartId");
                            if(object.toString().contains("DepName")){
                                UConstants.DepName = object.getString("DepName");
                            }
                            if(object.toString().contains("ComName")){
                                UConstants.ComName = object.getString("ComName");
                            }
//                            intent.setClass(this, MainActivity.class);
                            msg="登录成功！";
                            startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
                            finish();
                        }else {
                            msg = object.getString("msg");
                            startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                            finish();
                        }

                        Toast.makeText(x.app(), msg, Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                        finish();
                        e.printStackTrace();
                    }
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Util.printLog("getMerchants------------onError-----", ex.getMessage() + "");

                Toast.makeText(x.app(), "地址错误或服务器无法连接！", Toast.LENGTH_LONG).show();
                startActivity(new Intent(WelcomeActivity.this, LoginActivity.class));
                finish();
            }

            @Override
            public void onCancelled(CancelledException cex) {

                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
                Util.printLog("getMerchants------------onCancelled-----", cex.getMessage() + "");
            }

            @Override
            public void onFinished() {

            }
        });
    }}